/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

/**
 *
 * @author AAN
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.DbConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.Promo;
import model.ModelPromo;

public class LogicPromo {

    HttpServletRequest request;

    public LogicPromo(HttpServletRequest request) {
        this.request = request;
    }

    public String add() throws ParseException {
        String nama = request.getParameter("nama");
        String mulai = request.getParameter("mulai");
        String habis = request.getParameter("habis");
        String deskripsi = request.getParameter("deskripsi");
        String kategori = request.getParameter("kategori");
        String lokasi = request.getParameter("lokasi");

        DbConnection dbConn = new DbConnection();
        ModelPromo mdPromo = new ModelPromo(dbConn.getConnection());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        java.util.Date mulai_promo = formatter.parse(mulai);
        java.util.Date habis_promo = formatter.parse(habis);
        try {
            mdPromo.add(nama, mulai_promo, habis_promo, deskripsi, kategori, lokasi);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        //redirect method
        return listPromo();
    }

    public String listPromo() {
        //String idBarang = request.getParameter("idBarang");
        List<Promo> promo = new ArrayList<Promo>();
        DbConnection dbConn = new DbConnection();
        ModelPromo mdPromo = new ModelPromo(dbConn.getConnection());
        try {

            promo = mdPromo.getAllPromo();
            request.setAttribute("promo", promo);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "promo.jsp";
    }
}
